﻿using System;
using System.Net;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

namespace RFW.Weather
{
	public class Alerts : Communication
	{
		private List<Alert> _alerts = null;

		public Alerts() 
			: this("49120")
		{
		}

		public Alerts(string locationCode)
			: base(locationCode, "http://api.wunderground.com/auto/wui/geo/AlertsXML/index.xml?query={0}", true)
		{
		}

		public List<Alert> Items
		{
			get { return _alerts; }
		}

		public override void Process(XDocument results)
		{
			_alerts =
				(
					from alert in results.Element("alerts").Elements("alert")
					select new Alert
					{
						Type = alert.Element("AlertItem").Element("type").Value,
						Description = alert.Element("AlertItem").Element("description").Value,
						DateTime = alert.Element("AlertItem").Element("date").Value,
						Expires = alert.Element("AlertItem").Element("expires").Value,
						Message = alert.Element("AlertItem").Element("message").Value
					}
				).ToList();
		}

        public override void Process(string results)
        {
        }

		internal override void Failed()
		{
			_alerts = null;
		}
	}
}
